﻿using System;
using System.Collections.Generic;
using System.Text;
using Silk.NET.OpenGLES;
using TTEngine.Graphics.Render.PackedTexture;
using TTEngine.Resource;
using TTEngine.TTMath;
using static TTEngine.Graphics.SingleTextureBatcher;

namespace TTEngine.Graphics
{
    public enum BatcherType
    {
        None,
        SingleTexture,
        PackedTexture,
    }
    public class Batcher : IBatcher
    {

        SingleTextureBatcher singleBatcher;
        PackedTextureBatcher packedBatcher;
        PackedTextureInstanceBatcher packedInstBatcher;
        IBatcher lastBatcher;
        ScreenClipper clipper;
        public Batcher(CommonResource resource)
        {
            singleBatcher = new SingleTextureBatcher(resource);
            packedBatcher = new PackedTextureBatcher(resource.Package);
            packedInstBatcher = new PackedTextureInstanceBatcher(resource.Package);
            packedInstBatcher.SetPackElement(resource.PackedTextureGroup);
            lastBatcher = null;
            clipper = new ScreenClipper();
        }
        public bool ZTest
        {
            get
            {
                return singleBatcher.ZTest;
            }
            set
            {
                singleBatcher.ZTest = value;
                packedBatcher.ZTest = value;
            }
        }
        public bool ZWrite
        {
            get
            {
                return packedBatcher.ZWrite;
            }
            set
            {
                singleBatcher.ZWrite = value;
                packedBatcher.ZWrite = value;
            }
        }
        public SingleTextureBatcher GetSingleTextureBatcher()
        {
            if (singleBatcher != lastBatcher)
            {
                lastBatcher?.EndDraw();
                lastBatcher = singleBatcher;
                lastBatcher.BeginDraw();

            }
            return singleBatcher;
        }
        public PackedTextureBatcher GetPackedTextureBatcher()
        {
            if (packedBatcher != lastBatcher)
            {
                lastBatcher?.EndDraw();
                lastBatcher = packedBatcher;
                lastBatcher.BeginDraw();

            }
            return packedBatcher;
        }
        public PackedTextureInstanceBatcher GetPackedTextureInstanceBatcher()
        {
            if (packedInstBatcher != lastBatcher)
            {
                lastBatcher?.EndDraw();
                lastBatcher = packedInstBatcher;
                lastBatcher.BeginDraw();
            }
            return packedInstBatcher;
        }
        public void BeginDraw()
        {

        }
        public void EndDraw()
        {
            lastBatcher?.EndDraw();
            lastBatcher = null;
        }

        public void PushLimitRect(fAABB rect)
        {
            if (Runtime.activeTarget.IsMainOutput())
            {
                clipper.Push(new SizeRect((int)rect.Width, (int)rect.Height, (int)rect.x1, (int)Runtime.activeTarget.Height - (int)rect.y1 - (int)rect.Height));
            }
            else
            {
                clipper.Push(new SizeRect((int)rect.Width, (int)rect.Height, (int)rect.x1, (int)rect.y1));
            }
            clipper.ApplyScissor();
        }
        public void PopLimitRect()
        {
            clipper.Pop();
            clipper.ApplyScissor();
        }
    }
}
